﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Speech.Synthesis;
using DTO;
using BUS;

namespace Support_Study_English
{
    public partial class frmTestVocabulary : Office2007Form
    {
        #region Attributes
        private const int MAX_CTRLTEST = 10;
        private List<CtrlTestVocabulary> lsCtrlTestVocabulary;
        private List<VOCABULARY> _lsVcSource;
        private List<Dictionary<string,string>> _lsVcWrote;
        private int curStarIndex;
        public SpeechSynthesizer _speaker;
        public int typeReview;
        #endregion

        #region Properties
        public List<VOCABULARY> LsVcSource
        {
            get { return _lsVcSource; }
            set { _lsVcSource = value; }
        }
        #endregion
        public void DrawCtrlTestVocabulary()
        {
            for (int i = 0; i < MAX_CTRLTEST; i++)
            {
                CtrlTestVocabulary ctrlTestVocabulary = new CtrlTestVocabulary();
                ctrlTestVocabulary.AutoSize = true;
                ctrlTestVocabulary.BackColor = System.Drawing.Color.Transparent;
                ctrlTestVocabulary.Dock = System.Windows.Forms.DockStyle.Top;
                //ctrlTestVocabulary.Location = new System.Drawing.Point(0, i * 42);
                ctrlTestVocabulary.Name = "ctrlTestVocabulary" + (i+1).ToString();
                //ctrlTestVocabulary.Size = new System.Drawing.Size(812, 42);
                ctrlTestVocabulary.TabIndex = MAX_CTRLTEST - i;
                ctrlTestVocabulary.typeReview = typeReview;
                this.panel1.Controls.Add(ctrlTestVocabulary);
                lsCtrlTestVocabulary.Add(ctrlTestVocabulary);                
            }            
        }

        public frmTestVocabulary()
        {
            InitializeComponent();
            lsCtrlTestVocabulary = new List<CtrlTestVocabulary>();                        
            curStarIndex = 0;            
        }
       
        private void pbBack_Click(object sender, EventArgs e)
        {
            //foreach(CtrlTestVocabulary item in lsCtrlTestVocabulary)
            //{
            //    item.labResult.Visible = true;
            //    item.pbResult.Visible = true;                
            //}
            curStarIndex -= MAX_CTRLTEST;
            LoadDataCtrlTest();
        }

        private void pbReload_Click(object sender, EventArgs e)
        {

        }

        private void pbForward_Click(object sender, EventArgs e)
        {
            curStarIndex += MAX_CTRLTEST;
            LoadDataCtrlTest();
        }

        private void InitLsVcWrote()
        {
            _lsVcWrote = new List<Dictionary<string, string>>();
            for (int i = 0; i < _lsVcSource.Count; i++)
            {
                Dictionary<string, string> item = new Dictionary<string, string>();
                item.Add("WordWrote", "");
                item.Add("MeanEngWrote", "");
                item.Add("MeanViWrote", "");
                item.Add("Result", "false");
                _lsVcWrote.Add(item);
            }
        }

        private void UpdateLsVcWrote()
        {
            foreach (CtrlTestVocabulary item in lsCtrlTestVocabulary)
            {
                int indexOnLsVcWrote = item._indexOnListVcWrote;
                _lsVcWrote[indexOnLsVcWrote]["WordWrote"] = item.WordWrote;
                _lsVcWrote[indexOnLsVcWrote]["MeanEngWrote"] = item.MeanEngWrote;
                _lsVcWrote[indexOnLsVcWrote]["MeanViWrote"] = item.MeanViWrote;
            }
        }

        private void LoadDataCtrlTest()
        {            
            int nWord = _lsVcSource.Count;
            int starIndex = curStarIndex;
            int endIndex = curStarIndex + 10;
            this.btnFinish.Enabled = false;
            if (starIndex == 0)
            {
                this.btnBack.Enabled = false;
            }
            else
            {
                this.btnBack.Enabled = true;
            }

            if (endIndex >= nWord)
            {
                if (endIndex > nWord)
                {
                    for (int i = nWord; i < endIndex; i++)
                    {
                        lsCtrlTestVocabulary[i - curStarIndex].Visible = false;                        
                    }
                    endIndex = nWord;
                }                
                this.btnNext.Enabled = false;
                this.btnFinish.Enabled = true;
            }
            else
            {
                for (int i = 0; i < MAX_CTRLTEST; i++)
                {
                    lsCtrlTestVocabulary[i].Visible = true;
                }                
                this.btnNext.Enabled = true;
            }

            for (int i = endIndex - 1; i >= starIndex; i--)
            {
                lsCtrlTestVocabulary[i - curStarIndex].Vocabulary = _lsVcSource[endIndex - i + curStarIndex - 1];
                lsCtrlTestVocabulary[i - curStarIndex]._indexOnListVcWrote = endIndex - i + curStarIndex - 1;
                lsCtrlTestVocabulary[i - curStarIndex].labSTT.Text = (endIndex - i + curStarIndex).ToString() + ")";
                lsCtrlTestVocabulary[i - curStarIndex].LoadData();
                Dictionary<string, string> item = _lsVcWrote[endIndex - i + curStarIndex - 1];
                lsCtrlTestVocabulary[i - curStarIndex].LoadDataWrote(item["WordWrote"],
                    item["MeanEngWrote"], item["MeanViWrote"]);
                lsCtrlTestVocabulary[i - curStarIndex].Result = Boolean.Parse(item["Result"]);
                if (_isFinish)
                {
                    lsCtrlTestVocabulary[i - curStarIndex].ShowResult();
                }
            }            
        }

        protected void TestProcess()
        {
            for (int i = 0; i < _lsVcSource.Count; i++)
            {
                VOCABULARY itemSource = _lsVcSource[i];
                Dictionary<string,string> itemWrote = _lsVcWrote[i];
                if (ChuanHoaChuoi.ChuanHoa(itemWrote["WordWrote"]).ToUpper() == itemSource.Word.ToUpper() &&
                    ChuanHoaChuoi.ChuanHoa(itemWrote["MeanEngWrote"]).ToUpper() == itemSource.MeanEng.ToUpper() &&
                    ChuanHoaChuoi.ChuanHoa(itemWrote["MeanViWrote"]).ToUpper() == itemSource.MeanVi.ToUpper())
                {
                    itemWrote["Result"] = "true";
                }
                else
                {
                    itemWrote["Result"] = "false";
                }
            }
            //this.pbResult.Image = newImage;
        }    

        private void frmTestVocabulary_Load(object sender, EventArgs e)
        {
            InitLsVcWrote();
            LoadDataCtrlTest();
            foreach (CtrlTestVocabulary item in lsCtrlTestVocabulary)
            {
                item._speaker = _speaker;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            UpdateLsVcWrote();
            curStarIndex -= MAX_CTRLTEST;
            LoadDataCtrlTest();            
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            UpdateLsVcWrote();
            curStarIndex += MAX_CTRLTEST;
            LoadDataCtrlTest();
        }

        private bool _isFinish = false;
        private void btnFinish_Click(object sender, EventArgs e)
        {            
            TestProcess();
            _isFinish = true;
            curStarIndex = 0;            
            LoadDataCtrlTest();            
        }          
    }
}
